﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace THUANVV.UI
{
    public partial class frmInsertStudents : Form
    {
        public frmInsertStudents()
        {
            InitializeComponent();
        }

        private void frmInsertStudents_Load(object sender, EventArgs e)
        {
            SqlConnection conn=new SqlConnection(@"Data Source=TIMVENGAYHOMWA\SQL2008;Initial Catalog=XUAT_NHAP TON;Persist Security Info=True;User ID=sa;Password=.");
            SqlCommand cmd = new SqlCommand("dbo.SINHVIEN_GetAll", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            grdShow.DataSource = dt;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát chương trình không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information)==DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            string str="";
            if (rdoNam.Checked)
            {
                str = "Nam";
            }
            else
            {
                str = "Nữ";
            }
            SqlConnection conn = new SqlConnection(@"Data Source=TIMVENGAYHOMWA\SQL2008;Initial Catalog=XUAT_NHAP TON;Persist Security Info=True;User ID=sa;Password=.");
            SqlCommand cmd = new SqlCommand("dbo.SINHVIEN_Insert", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@MASV",txtMa.Text));
            cmd.Parameters.Add(new SqlParameter("@TENSV", txtTen.Text));
            cmd.Parameters.Add(new SqlParameter("@GIOITINH", str));
            cmd.Parameters.Add(new SqlParameter("@NGAYSINH", dateTimePicker1.Value));
            cmd.Parameters.Add(new SqlParameter("@QUEQUAN", txtQue.Text));
            cmd.Parameters.Add(new SqlParameter("@EMAIL", txtEmail.Text));
            cmd.Parameters.Add(new SqlParameter("@SDT", txtSDT.Text));
            conn.Open();
            string id = (string)cmd.ExecuteScalar();
            conn.Close();
            frmInsertStudents_Load(sender, e);
            txtMa.Clear();
            txtTen.Clear();
            txtSDT.Clear();
            txtQue.Clear();
            txtEmail.Clear();
            //conn.Open();
            //string id = (string)cmd.ExecuteScalar();
            //conn.Close();
            string kq= String.Format("Bạn vừa nhập mã sinh viên là: {0}",id);
            MessageBox.Show(kq, "Thông báo", MessageBoxButtons.OK);

        }
    }
}
